/**
 * 
 */
package design_pattern.proxy.searcher;

/**
 * @author linzq
 * 
 */
public class Proxy implements Searcher {

  RealSearcher searcher;

  UsageLogger logger;

  AccessValidator validator;

  public Proxy() {
    searcher = new RealSearcher();
  }

  @Override
  public String doSearch(String userId, String searchType) {
    if (checkValidate(userId)) {
      String result = searcher.doSearch(null, searchType);
      logUser(userId);
      return result;
    }
    return null;
  }

  private boolean checkValidate(String userId) {
    validator = new AccessValidator();
    return validator.validate(userId);
  }

  private void logUser(String userId) {
    UsageLogger logger = new UsageLogger(userId);
    logger.save(userId);
  }
}
